package com.qc31.testjarjava;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.syqc.login.ILoginCallback;
import com.syqc.login.LoginUtil;
import com.syqc.login.MD5Utils;
import com.syqc.monitor.ui.carmonitor.CarMonitorActivity;
import com.syqc.monitor.ui.carsmonitor.MultiVehicleActivity;
import com.syqc.monitor.ui.historyline.HistoryLineActivity;
import com.syqc.monitor.ui.list.ListMonitorActivity;
import com.syqc.monitor.ui.mile.MileActivity;
import com.syqc.monitor.ui.newaddcar.AddCarActivity;
import com.syqc.monitor.ui.photo.PhotosActivity;
import com.syqc.monitor.ui.realalarm.RealAlarmActivity;
import com.syqc.monitor.ui.simplemonitor.SimpleMonitorActivity;
import com.syqc.monitor.ui.singlemap.SingleMapActivity;
import com.syqc.monitor.ui.stopcar.StopCarActivity;
import com.syqc.sensor.demp.DampActivity;
import com.syqc.sensor.oil.ReportOilActivity;
import com.syqc.sensor.recent.RecentOilActivity;
import com.syqc.sensor.tdemp.ReportTempActivity;
import com.syqc.sensor.water.ReportWaterActivity;
import com.syqc.sensor.weight.ReportWeightActivity;
import com.syqc.videos.VideotapeActivity;
import com.syqc.videos.player.RealPlayerActivity;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private final ArrayList<DemoInfo> mActs = new ArrayList<>();
    private boolean isLogin = false;
    private String errorMsg = "请先登录";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setOnclick();
        initAct();
        initListView();
    }

    private void initListView() {
        DemoListAdapter adapter = new DemoListAdapter(this, mActs);
        ListView listView = findViewById(R.id.listView);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener((parent, view, position, id) -> {
            if (isLogin) {
                startActivity(new Intent(MainActivity.this, mActs.get(position).getClazz()));
            } else {
                Toast.makeText(MainActivity.this, errorMsg, Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void setOnclick() {
        findViewById(R.id.btnLogin).setOnClickListener(v ->
                LoginUtil.INSTANCE.login("106.14.186.44", "9999", "ys_admin", MD5Utils.Companion.getmd5("123456")
                        , new ILoginCallback() {
                            @Override
                            public void onSuccess() {
                                isLogin = true;
                            }

                            @Override
                            public void onFailure(@NonNull String s) {
                                isLogin = false;
                                errorMsg = s;
                                Toast.makeText(MainActivity.this, errorMsg, Toast.LENGTH_SHORT).show();
                            }
                        }, false));
    }

    private void initAct() {
        mActs.add(new DemoInfo("单车监控", CarMonitorActivity.class));
        mActs.add(new DemoInfo("多车监控", MultiVehicleActivity.class));
        mActs.add(new DemoInfo("列表监控", ListMonitorActivity.class));
        mActs.add(new DemoInfo("极简监控", SimpleMonitorActivity.class));
        mActs.add(new DemoInfo("历史轨迹", HistoryLineActivity.class));
        mActs.add(new DemoInfo("实时报警", RealAlarmActivity.class));
        mActs.add(new DemoInfo("实时视频", RealPlayerActivity.class));
        mActs.add(new DemoInfo("录像回放", VideotapeActivity.class));
        mActs.add(new DemoInfo("单车监控", SingleMapActivity.class));
        mActs.add(new DemoInfo("里程统计", MileActivity.class));
        mActs.add(new DemoInfo("照片查询", PhotosActivity.class));
        mActs.add(new DemoInfo("停车统计", StopCarActivity.class));
        //下面部分为sensor-sdk包含
        mActs.add(new DemoInfo("油量统计", ReportOilActivity.class));
        mActs.add(new DemoInfo("水位统计", ReportWaterActivity.class));
        mActs.add(new DemoInfo("温度统计", ReportTempActivity.class));
        mActs.add(new DemoInfo("湿度统计", DampActivity.class));

        mActs.add(new DemoInfo("载重统计", ReportWeightActivity.class));
        mActs.add(new DemoInfo("近期油耗", RecentOilActivity.class));
        mActs.add(new DemoInfo("新增车辆", AddCarActivity.class));
    }


    class DemoListAdapter extends BaseAdapter {
        private ArrayList<DemoInfo> infos;
        private Context context;

        DemoListAdapter(Context context, ArrayList<DemoInfo> infos) {
            this.infos = infos;
            this.context = context;
        }

        @Override
        public int getCount() {
            return infos.size();
        }

        @Override
        public Object getItem(int position) {
            return infos.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = View.inflate(context, R.layout.demo_list_item, null);
            }
            TextView textView = convertView.findViewById(R.id.tvTitle);
            textView.setText(infos.get(position).getTitle());
            return convertView;
        }
    }
}